package com.example.myapplication2

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

class MainActivity : AppCompatActivity() {

    val app by lazy { application as App }

    var fileName: File? = null

    val writeLog by lazy { LogWriter(fileName!!) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        checkAndRequestNotificationPermission()

        fileName = getExternalFilesDir(null)!!.absoluteFile
        Log.e("tag-->", "data---$fileName")


//        val viewModlue = ViewModelProvider(this)[ShareViewModlue::class.java]
//        viewModlue.setData()
//        viewModlue.mShareData.observe(this) {
//            println("shareviewmodule data-->${it}")
//        }


        app.mShareModule.setData()
        app.mShareModule.mShareData.observe(this) {
            println("shareviewmodule data-->${it}")
        }


    }


    override fun onStart() {
        super.onStart()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }


    override fun onResume() {
        super.onResume()
//        app.mShareModule.mShareData.observe(this) {
//            println("shareviewmodule111 data-->${it}")
//        }

//        val sDate = app.mShareModule.mShareData.value
//        Log.e("data-->11","$sDate")

    }


    fun bt1(v: View) {
        val test = singleClass
        println("weither only--->$test")
        startActivity(Intent(this, aActivity::class.java))


    }

    fun bt2(v: View) {

    }


    fun bt3(v: View) {

    }


    fun bt4(v: View) {

    }


    private fun checkAndRequestNotificationPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
            != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
        }
    }

    private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            Log.e("PermissionCheck", "通知权限已授予")
        } else {
            Log.e("PermissionCheck", "通知权限未授予")
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun refreash(event: ReashEvent) {
        app.mShareModule.mShareData.observe(this) {
            println("shareviewmodule111 data-->${it}")
        }
    }


}